#include "SimpleTable.h"

#include <cassert>

#include "memdb/impl/simple/Data.h"

namespace memdb {
namespace impl {
namespace simple {

SimpleTable::SimpleTable(IMeta & meta, std::unique_ptr<IData> data)
: _meta(meta)
, _data(std::move(data))
{
}
SimpleTable::~SimpleTable(){
}

std::error_code SimpleTable::get(const KeyType & key, std::string & value) const { 
  return _data->get(key, value);
}
std::error_code SimpleTable::insert(const KeyType & key, const KeyType & value) { 
  return _data->insert(key, value);
}
std::error_code SimpleTable::remove(const KeyType & key) { 
  return _data->remove(key);
}

std::error_code SimpleTable::update(const KeyType & key, const KeyType & value) { 
  return _data->update(key, value);
}

std::error_code SimpleTable::replace(const KeyType & key, const KeyType & value) { 
  return _data->replace(key, value);
}

std::error_code SimpleTable::update(const KeyType & key, const KeyType & value, std::string & old) { 
  if(auto && ec = _data->get(key, old); ec){// ec != 0
    return ec;
  }
  return _data->update(key, value);
}
std::pair<std::error_code, bool> SimpleTable::replace(const KeyType & key, const KeyType & value, std::string & old) { 
  if(auto && ec = _data->get(key, old); ec){// ec != 0
    return std::make_pair(_data->insert(key, value), false);
  }
  return std::make_pair(_data->replace(key, value), true);
}

size_t SimpleTable::count() const { 
  return _data->count();
}


std::unique_ptr<ITable::Iterator> SimpleTable::iterator() const { 
  return _data->iterator();
}
std::unique_ptr<ITable::Iterator> SimpleTable::range(size_t offset, size_t len) const { 
  return _data->iterator(offset, len);
}
std::unique_ptr<ITable::Iterator> SimpleTable::findRange(const KeyType & key, size_t offset, size_t len) const { 
  return _data->iterator(key, offset, len);
}


}//namespace simple
}//namespace impl
}//namespace memdb
